﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is AST'r Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <vegas@ekameleon.net>.
  Portions created by the Initial Developer are Copyright (C) 2004-2008
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
 */
import flash.filters.DropShadowFilter;

import andromeda.model.ModelCollector;

import asgard.display.ConfigurableDisplayObject;
import asgard.events.LocalizationEvent;
import asgard.media.SoundLibrary;
import asgard.system.Localization;
import asgard.text.StyleModel;

import astro.display.UIList;
import astro.model.ModelList;

import lunas.display.button.SimpleButtonDisplay;

import pegas.colors.BasicColor;
import pegas.events.ButtonEvent;

import vegas.events.Delegate;
import vegas.string.HTMLStringFormatter;

/**
 * This display control the display reference on the root of the application.
 * @author eKameleon
 */
class astro.display.application.MyDisplay extends ConfigurableDisplayObject 
{

	/**
	 * Creates a new MyDisplay instance.
	 * @param target the MovieClip reference of this display.
	 */	
	public function MyDisplay( target:MovieClip ) 
	{
		
		super( UIList.MY_DISPLAY, target );
		
		Localization.getInstance().addEventListener(Localization.CHANGE, new Delegate(this, changeLocalization)) ;
		
		// views
		
		background = resolve( "background" , 1 ) ;
		titleField = resolve( "titleField" , 2) ;
		button     = new SimpleButtonDisplay( null, resolve("button", 3 ) ) ;
		
		// behaviours
		
		button.addEventListener( ButtonEvent.CLICK , new Delegate(this, click) ) ;
		button.addEventListener( ButtonEvent.OVER , new Delegate(this, over) ) ;
		
		titleField.selectable = false ;
		titleField.html = true ;
		titleField.styleSheet = StyleModel( ModelCollector.get( ModelList.STYLE_MODEL ) ).getStyleSheet() ;
		
		// init
		
		Stage.addListener(this) ;
		
		onResize = Delegate.create(this, update) ;
		
		hide() ;
		
	}
	
	/**
	 * The background of the display.
	 */
	public var background:MovieClip ;
	
	/**
	 * The button of the display.
	 */
	public var button:SimpleButtonDisplay ;

	/**
	 * The color of the background.
	 */
	public var color:Number = null ;
	
	/**
	 * The drop shadow reference of this display.
	 */
	public var shadow:DropShadowFilter = new DropShadowFilter( 4, 45, 0x000000, 80, 8, 8, 1, 3 ) ;

	/**
	 * The sound id in the SoundLibrary use when the user click the button.
	 */
	public var soundClick:String ;

	/**
	 * The sound id in the SoundLibrary use when the user is over the button.
	 */
	public var soundOver:String ;

	/**
	 * The style name of the title field.
	 */
	public var style_name_title:String = "my_display_title" ;

	/**
	 * Returns the title value of the display.
	 * @return the title value of the display. 
	 */
	public function get title():String
	{
		return _title ;
	}
	
	/**
	 * Returns the title value of the display.
	 * @return the title value of the display. 
	 */
	public function set title( str:String ):Void
	{
		_title = str || "" ;
		titleField.htmlText = HTMLStringFormatter.paragraph( _title , style_name_title ) ;
	}

	/**
	 * The title field of this display.
	 */
	public var titleField:TextField ;

	/**
	 * Change the localization of this display.
	 */
	public function changeLocalization( e:LocalizationEvent ):Void
	{
		e.init(this, name) ;
	}
	
	/**
	 * Click the button.
	 */
	public function click( e:ButtonEvent ):Void
	{
		getLogger().info( this + " " + e ) ;		
		SoundLibrary(ModelCollector.get(ModelList.SOUND_MODEL) ).getSound( soundClick ).start() ;
	}
	
	/**
	 * Over the button.
	 */
	public function over( e:ButtonEvent ):Void
	{
		getLogger().info( this + " " + e ) ;	
		SoundLibrary(ModelCollector.get(ModelList.SOUND_MODEL) ).getSound( soundOver ).start() ;
	}

	/**
	 * Update the display.
	 */
	public function update():Void
	{
		
		titleField.htmlText = HTMLStringFormatter.paragraph( title , style_name_title ) ;		
		
		x = (Stage.width - width ) / 2 ;
		y = (Stage.height - height ) / 2 ;

		if ( color == null )
		{
			(new BasicColor(background)).reset() ;
		}
		else
		{
			(new BasicColor(background)).setRGB(color) ;	
		}
		
		background.filters = [ shadow ] ;
	}

	private var onResize:Function ;
	private var _title:String = "" ;

}